import sys, os, subprocess

if len(sys.argv) > 1:
    path = sys.argv[1]
else:
    path = os.curdir
projects = {}

temp = os.listdir(path)
files = filter(lambda x: x.endswith(".txt"), temp);
#print path
#print files
for filename in files:
    elements = filename.split("_")
    if not projects.has_key(elements[0]):
        projects[elements[0]] = ["Name=\"" + elements[0] + "\""]
    #print filename
    process = subprocess.Popen(["execute_converter.bat", "convertor_"+elements[1]+".py", filename], \
                           stdout=subprocess.PIPE)
    lines = process.stdout.readlines()
    
    if len(lines) > 0:
        #print elements[0], lines[0].strip()        
        projects[elements[0]].append(lines[0].strip())
        projects[elements[0]].append(elements[1]+"_lnk=\"" + filename + "\"")
        #print projects[elements[0]]


print "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
print "<Projects>"
projectname = projects.keys()
#print "projs", projects
#print "projectname", projectname
for projectname in projectname:
    projStr = "    <Project "
    #print "proj", projectname
    for element in projects[projectname]:
        projStr = projStr + element + " "
    projStr = projStr + "/>"
    print projStr
    

print "</Projects>"

